package com.shareyi.simpleserver.servlet;

public class PatternUtil {

	//TODO 实现这个方法
	public static boolean isMatchPattern(String src,String pattern,int type){

		if(!src.startsWith("/")){
			src="/"+src;
		}

		switch (type) {
		case ServletConstant.PATTERN_ABSOLUTE:
				return src.equals(pattern);
		case ServletConstant.PATTERN_DIRECTORY:
			return src.startsWith(pattern.substring(0,pattern.length()-1));
		case ServletConstant.PATTERN_EXTEND:
			return src.endsWith(pattern.substring(pattern.lastIndexOf(".")));
		case ServletConstant.PATTERN_DEFAULT:
			return true;

		default:
			break;
		}
		
		return false;
	}
	
	/**
	 * 获取正则类型type
	 * @param pattern
	 * @return
	 */
	public static Integer getPatternType(String pattern){
		int idx=pattern.indexOf("*");
		if(idx<0){ //完全匹配
			if(pattern.equals("/")){
				return ServletConstant.PATTERN_DEFAULT;
			}else if(!pattern.endsWith("/")){
				return ServletConstant.PATTERN_ABSOLUTE;
			}
		}else if(pattern.startsWith("*.")){ //扩展名匹配
			return ServletConstant.PATTERN_EXTEND;
		}else if(pattern.startsWith("/")&&pattern.endsWith("/*")){  //路径匹配
			return ServletConstant.PATTERN_DIRECTORY;
		}
		return ServletConstant.PATTERN_ILLAGAL;
		
	}
	
}
